package com.dream.technology.service.impl;

import com.dream.technology.components.riskControl.tianji.RcConstan;
import com.dream.technology.db.dao.RcRuleItemMapper;
import com.dream.technology.db.entity.RcRuleItem;
import com.dream.technology.db.entity.RcRuleItemQuery;
import com.dream.technology.service.RcRuleItemService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("rcRuleItemService")
public class RcRuleItemServiceImpl implements RcRuleItemService {

    @Autowired
    RcRuleItemMapper rcRuleItemMapper;

    @Override
    public int countByExample(RcRuleItemQuery example) {
        return rcRuleItemMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(RcRuleItemQuery example) {
        return rcRuleItemMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return rcRuleItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(RcRuleItem record) {
        return rcRuleItemMapper.insert(record);
    }

    @Override
    public int insertSelective(RcRuleItem record) {
        return rcRuleItemMapper.insertSelective(record);
    }

    @Override
    public List<RcRuleItem> selectByExample(RcRuleItemQuery example) {
        return rcRuleItemMapper.selectByExample(example);
    }

    @Override
    public RcRuleItem selectByPrimaryKey(Long id) {
        return rcRuleItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(RcRuleItem record, RcRuleItemQuery example) {
        return rcRuleItemMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(RcRuleItem record, RcRuleItemQuery example) {
        return rcRuleItemMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(RcRuleItem record) {
        return rcRuleItemMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(RcRuleItem record) {
        return rcRuleItemMapper.updateByPrimaryKey(record);
    }


    /**
     * 查询指定id的规则集合
     * 配置规则集验证
     *
     * @param ids
     * @return
     */
    @Override
    public List<RcRuleItem> selectByIdInList(List<Long> ids) {
        return rcRuleItemMapper.selectByIdInList(ids);
    }


    /**
     * 查询指定ruleId的规则配置
     *
     * @param ruleId
     * @return
     */
    @Override
    public List<RcRuleItem> selectByRuleId(Long ruleId) {
        RcRuleItemQuery query = new RcRuleItemQuery();
        RcRuleItemQuery.Criteria criteria = query.createCriteria();
        criteria.andRuleIdEqualTo(ruleId);
        List<RcRuleItem> rcRuleItems = rcRuleItemMapper.selectByExample(query);
        return rcRuleItems;
    }

    /**
     * copy 指定ruleId的规则配置
     *
     * @param ruleId
     * @param nweRuleId
     * @return
     */
    @Override
    public String copy(Long ruleId, Long nweRuleId) {
        List<RcRuleItem> rcRuleItems = selectByRuleId(ruleId);
        if (CollectionUtils.isEmpty(rcRuleItems)) {
        	return RcConstan.SUCCESS;
        }
        ArrayList<RcRuleItem> ruleItemArrayList = new ArrayList<>();
        rcRuleItems.forEach(rcRuleItem -> {
            rcRuleItem.setRuleId(nweRuleId);
            rcRuleItem.setId(null);
           ruleItemArrayList.add(rcRuleItem);
        });
        insertList(ruleItemArrayList);
        return RcConstan.SUCCESS;
    }

    /**
     * 批量插入item
     *
     * @param record
     * @return
     */
    @Override
    public int insertList(List<RcRuleItem> record) {
        return rcRuleItemMapper.insertList(record);
    }


}
